package com.thenie.web;

import com.thenie.pojo.Book;
import com.thenie.pojo.Page;
import com.thenie.service.BookService;
import com.thenie.service.impl.BookServiceImpl;
import com.thenie.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet {
    BookService bookService=new BookServiceImpl();
    protected void page(HttpServletRequest req,HttpServletResponse resp){
            try {
                Page<Book> bookPage;
                //1.获取当前页数，以及当前页数容量
                int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1);
                int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
                bookPage = bookService.page(pageNo, pageSize);
                bookPage.setUrl("client/bookServlet?action=page");
                req.setAttribute("bookPage",bookPage);
                req.getRequestDispatcher("/pages/client/index.jsp").forward(req, resp);
            } catch (ServletException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
    protected void pageByPrice(HttpServletRequest req,HttpServletResponse resp){
        try {
            //1.获取请求参数，价格范围（min，max），请求页数，页面容量
            int minPrice= Integer.parseInt(req.getParameter("minPrice"));
            int maxPrice= Integer.parseInt(req.getParameter("maxPrice"));
            int pageNo= WebUtils.parseInt(req.getParameter("pageNo"),1);
            int pageSize=WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);
            //2.求页面内容
            Page<Book> bookPage = bookService.pageByPrice(pageNo, pageSize, minPrice, maxPrice);
            bookPage.setUrl("client/bookServlet?action=pageByPrice");
            //3.请求转发
            req.setAttribute("bookPage",bookPage);
            req.getRequestDispatcher("/pages/client/index.jsp").forward(req, resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
